package GreedyAlgorithmPackage;

import org.junit.jupiter.api.Test;

import java.util.Arrays;
import java.util.Comparator;

/**
 * @author Lzm
 * @version 1.0
 */
public class eraseOverlapIntervals_ {
//int[][] points = new int[][]{{10,16},{2,8},{1,6},{7,12}};

  @Test
  public void eraseOverlapIntervalsTest(){
    int[][] points = new int[][]{{1,2},{2,3},{3,4},{1,3}};
    Arrays.sort(points, new Comparator<int[]>() {
      //从小到大排列
      @Override
      public int compare(int[] o1, int[] o2) {
        return Integer.compare(o1[0] , o2[0]);
      }
    });
    for (int[] point : points) {
      StringBuilder sb = new StringBuilder();
      sb.append(point[0]).append(",").append(point[1]);
      System.out.println(sb.toString());
    }
    System.out.println(eraseOverlapIntervals(points));
  }
  public int eraseOverlapIntervals(int[][] intervals) {
    Arrays.sort(intervals, new Comparator<int[]>() {
      //从小到大排列
      @Override
      public int compare(int[] o1, int[] o2) {
        return Integer.compare(o1[0] , o2[0]);
      }
    });
    int count = 0;
    for (int i = 1; i < intervals.length ; i++){
      if (intervals[i][0] < intervals[i - 1][1]){
        count++;
        intervals[i][1] = Math.min(intervals[i][1],intervals[i - 1][1]);
      }
      else{
        continue;
      }
    }
    return count;
  }
}
